package kiwi.assist.dev.reflect {
	import fl.core.UIComponent;
	import fl.data.DataProvider;
	import fl.managers.StyleManager;

	import ghostcat.gxml.spec.DisplaySpec;

	import kiwi.debug.Log;

	import flash.utils.getDefinitionByName;

	/**
	 * ui组件专用的
	 * 一个简单的GreenButton按钮的序列化
	 * 
	 * @author zhangming.luo
	 */
	public class UiComponentSpec extends DisplaySpec {
		override public function addChild(source : *, child : *, xml : XML) : void {
			if (source is UIComponent) {
				var component : UIComponent = source;
				if (xml.localName().toString() == "style") {
					component.setStyle(String(xml.@name), readStyleValue(xml, child));
				} else if (xml.localName().toString() == "dataProvider") {
					// 数据的赋值
					component["dataProvider"] = new DataProvider(child);
				} else {
					super.addChild(source, child, xml);
				}
			} else {
				super.addChild(source, child, xml);
			}
		}

		public static function readStyleValue(xml : XML, child : Array) : * {
			// 样式的赋值
			var value : *;
			if (xml.attribute("format").length() == 0) {
				Log.error("没有设置格式 " + xml.toXMLString());
			} else if (xml.@format == "Numeric") {
				value = Number(child[0]);
			} else if (xml.@format == "Boolean") {
				value = Boolean(child[0]);
			} else if (xml.@format == "Class") {
				value = getDefinitionByName(child[0]);
			} else if (xml.@format == "Style") {
				value = child[0];
			} else if (xml.@format == "TextFormat") {
				value = child[0];
			} else if (xml.@format == "Filter") {
				value = child[0];
			} else if (xml.@format == "Skin") {
				// 寻找对应的样式
				value = StyleManager.getStyle("#" + child[0] + "#");
				if (value == null) {
					// 样式库内没有, 就还原真相
					value = child[0];
				}
			} else if (xml.@format == "Array") {
				// 数组的形式
				value = child;
			} else {
				value = child[0];
			}
			return value;
		}
	}
}
